////////////////////////////////////////////////////////////////////////////////
//
//  FLYING KIWI BV
//  Copyright 2010 Flying Kiwi BV
//  All Rights Reserved.
//
//  NOTICE: Flying Kiwi permits you to use, modify, and distribute this file
//  in accordance with the terms of the license agreement accompanying it.
//
////////////////////////////////////////////////////////////////////////////////
package plantool.data.sqlite {
	//--------------------------------------------------------------------------
	//  Imports
	//--------------------------------------------------------------------------
	import flash.data.SQLConnection;
	import flash.events.SQLEvent;
	import flash.filesystem.File;
	
	import plantool.data.DatabaseController;
	import plantool.data.FieldTypes;
	import plantool.data.IDatabaseController;
	import plantool.data.Table;
	
	//--------------------------------------------------------------------------
	//  [Metadata (Events, Style, Effects etc)]
	//--------------------------------------------------------------------------
	
	//--------------------------------------------------------------------------
	//  Class declaration
	//--------------------------------------------------------------------------
	public class SQLiteController extends DatabaseController implements IDatabaseController {
		//--------------------------------------------------------------------------
		//Class constants
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//Class variables
		//--------------------------------------------------------------------------
		private var _connection:SQLConnection		= null;
		
		//--------------------------------------------------------------------------
		//Class properties
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//Constructor and Destructor
		//--------------------------------------------------------------------------
		public function SQLiteController() {
			super();
		}
		
		//--------------------------------------------------------------------------
		//Class methods
		//--------------------------------------------------------------------------
		private function onOpen_connection(e:SQLEvent):void {
			//remove open-event listener
			_connection.removeEventListener(SQLEvent.OPEN		, onOpen_connection);
			
			//call onConnected
			onConnected();
		}
		
		public override function close():void {
			_connection.close();
		}
		
		public override function connect():void {
			//create the database file if it doesnt exist
			var dbaseFile:File = File.applicationStorageDirectory.resolvePath("data.sqlite");
			
			//attempt to connect to the database file
			_connection	= new SQLConnection();
			_connection.addEventListener(SQLEvent.OPEN		, onOpen_connection);
			_connection.openAsync(dbaseFile);
		}
		
		public override function createDateFromTimestamp(timestamp:Number):Date {
			return new Date(timestamp * 1000);
		}
		
		public override function createTable(table:Table, onCompleteHandler:Function):void {
			var tableHelper:CreateTableHelper = new CreateTableHelper(onCompleteHandler);
			tableHelper.create(table);
		}
		
		public override function formatDateField(field:String):String {
			return "strftime('%s', " + field + ")";
		}
		
		public override function formatDate(date:Date):String {
			return "datetime(" + date.valueOf() / 1000 + ", 'unixepoch')";
		}
		
		public override function getFieldName(type:uint):String {
			switch (type) {
				case FieldTypes.VARCHAR:
					return "VARCHAR";
					break;
				case FieldTypes.TEXT:
					return "TEXT";
					break;
				case FieldTypes.INT:
					return "INTEGER";
					break;
				case FieldTypes.DATE:
					return "DATETIME";
				case FieldTypes.FLOAT:
					return "FLOAT";
				case FieldTypes.BOOLEAN:
					return "BOOL";
				case FieldTypes.UNKNOWN:
				default:
					throw new Error("!!UNKNOWN FIELD TYPE!! - Use FieldDefenitions class.");
					break;
			}
			return "";
		}
		
		public override function query(q:String, onCompleteHandler:Function, onErrorHandler:Function, itemClass:Class = null, userdata:* = null):void {
			//create sqlite query
			var query:SQLiteQuery		= new SQLiteQuery(_connection, q, onCompleteHandler, onErrorHandler, itemClass, userdata);
		}
		
		public override function transaction(querys:Array, onCompleteHandler:Function, onErrorHandler:Function, userdata:* = null):void {
			var transaction:SQLiteTransaction = new SQLiteTransaction(_connection, querys, onCompleteHandler, onErrorHandler);
		}
	}
}